package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.EmployeeCreateException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.LoginException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //密码比对
        // 后期需要进行md5加密，然后再进行比对
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!md5password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }

    @Override
    public void creat(EmployeeDTO employeeDTO) {
        // 1.校验用户名
        Integer count = employeeMapper.selectCountByName(employeeDTO.getUsername());
        if (count>0){
            throw new EmployeeCreateException(MessageConstant.REPEAT_USER_NAME);
        }
        // 2.校验手机号
        if (Objects.isNull(employeeDTO.getPhone())  || employeeDTO.getPhone().length() != 11){
            throw new EmployeeCreateException(MessageConstant.ERROR_PHONE);
        }
        // 3.校验身份证
        if (Objects.isNull(employeeDTO.getIdNumber())  || employeeDTO.getIdNumber().length() != 18){
            throw new EmployeeCreateException(MessageConstant.ERROR_ID_NUMBER);
        }
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        /* 重复属性进行优化
        employee.setUsername(employeeDTO.getUsername());
        employee.setName(employeeDTO.getName());
        employee.setPhone(employeeDTO.getPhone());
        employee.setSex(employeeDTO.getSex());
        employee.setIdNumber(employeeDTO.getIdNumber());*/

        String md5password = DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes());
        employee.setPassword(md5password);              // employee.setPassword("123456");    PasswordConstant.DEFAULT_PASSWORD="123456"(常量)
        employee.setStatus(StatusConstant.ENABLE);      // employee.setStatus(1);    StatusConstant.ENABLE=1(常量)
        // AOP切面自动注入时间
        employeeMapper.insert(employee);
    }

    @Override
    public PageResult page(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        List<Employee> employeeList = employeeMapper.getEmpListByName(employeePageQueryDTO.getName());
        PageInfo<Employee> pageInfo = PageInfo.of(employeeList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Employee emp = employeeMapper.getEmpById(id);
        emp.setStatus(status);
        employeeMapper.update(emp);
    }

    @Override
    public Employee queryById(Long id) {
        Employee emp = employeeMapper.getEmpById(id);
        emp.setPassword(null);
        return emp;
    }

    @Override
    public void updateEmp(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.update(employee);
    }

}
